//
//  XFAlertView.m
//  UkongM_1.7
//
//  Created by ukongm on 17/6/15.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFAlertView.h"
#import "Masonry.h"
@interface XFAlertView ()

/** 背景 */
@property(nonatomic,strong)UIView* backView;

/** 整个标题 */
@property(nonatomic,strong)UIView* titleView;
/** 标题 : (温馨提示)*/
@property(nonatomic,strong)UILabel* titleLb;
/** logo */
@property(nonatomic,strong)UIImageView* logoImageView;
/** 内容 */
@property(nonatomic,strong)UILabel* valueLb;
/** 下面的红色标题 */
@property(nonatomic,strong)UILabel* btmTitle;
/** 确定按钮 */
@property(nonatomic,strong)UIButton* fixedBtn;
/** 取消按钮 */
@property(nonatomic,strong)UIButton* canelBtn;

/** 线 */
@property(nonatomic,strong)UIView* lineView;


/** 展示的高度 */
@property(nonatomic,assign)CGFloat showHeight;

//保存下来的确定事件
@property(nonatomic,copy)void(^fixedComplate)();



@end


@implementation XFAlertView

- (UIButton *)fixedBtn{
    
    if (_fixedBtn == nil) {
        _fixedBtn = [[UIButton alloc] init];
        [_fixedBtn setTitle:@"确定" forState:UIControlStateNormal];
        _fixedBtn.backgroundColor = MAINCOLOR;
        [_fixedBtn addTarget:self action:@selector(fixedAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fixedBtn;
}

- (UIButton *)canelBtn{
    
    if (_canelBtn == nil) {
        _canelBtn = [[UIButton alloc] init];
        [_canelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _canelBtn.backgroundColor = [UIColor red:200 green:200 blue:200];
        [_canelBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    }
    return _canelBtn;
}

- (UILabel *)btmTitle{
    
    if (_btmTitle == nil) {
        _btmTitle = [[UILabel alloc] init];
        _btmTitle.textAlignment = NSTextAlignmentCenter;
        _btmTitle.font = [UIFont systemFontOfSize:17];
        _btmTitle.textColor = MAINCOLOR;
        _btmTitle.numberOfLines = 0;
    }
    return _btmTitle;
}

- (UIView *)titleView{
    
    if (_titleView == nil) {
        _titleView = [[UIView alloc] init];
    }
    return _titleView;
}

- (UILabel *)titleLb{
    
    if (_titleLb == nil) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.font = [UIFont systemFontOfSize:17];
        _titleLb.textColor = MAINCOLOR;
        _titleLb.textAlignment = NSTextAlignmentCenter;
        
        _titleLb.text = @"温馨提示";
        
    }
    return _titleLb;
}

- (UIView *)lineView{
    
    if (_lineView == nil) {
        _lineView = [[UIView alloc] init];
        
        _lineView.backgroundColor = MAINCOLOR;
    }
    return _lineView;
}

- (UIImageView *)logoImageView{
    
    if (_logoImageView == nil) {
        _logoImageView = [[UIImageView alloc] init];
        _logoImageView.image = [UIImage imageNamed:@"U&K－logo-01@3x"];
    }
    return _logoImageView;
}


- (UILabel *)valueLb{
    
    if (_valueLb == nil) {
        _valueLb = [[UILabel alloc] init];
        _valueLb.font = [UIFont systemFontOfSize:14];
        _valueLb.textColor = [UIColor red:120 green:120 blue:120];
        _valueLb.numberOfLines = 0;
        _valueLb.textAlignment = NSTextAlignmentCenter;
    }
    return _valueLb;
}

- (UIView *)backView{
    
    if (_backView == nil) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor blackColor];
        _backView.alpha = 0.7;
//        [_backView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)]];
    }
    return _backView;
}



+ (instancetype)alertWithValue:(NSString *)value fixedCompalte:(void (^)())fixedComplate type:(XFAlertViewShowType)type{
    return [self alertWithValue:value title:nil fixedCompalte:fixedComplate type:type];
}

+ (instancetype)alertWithValue:(NSString *)value title:(NSString *)title fixedCompalte:(void (^)())fixedComplate type:(XFAlertViewShowType)type{
    XFAlertView* alertView = [[XFAlertView alloc] initWithValue:value title:title fixedCompalte:fixedComplate type:type];
    
    return alertView;
}



- (instancetype)initWithValue:(NSString*)value title:(NSString*)title fixedCompalte:(void (^)())fixedComplate type:(XFAlertViewShowType)type;
{
    self = [super init];
    if (self) {
        
        self.fixedComplate = fixedComplate;
        self.backgroundColor = [UIColor whiteColor];
        self.alpha = 0.9;
        [self layerWithRadian:8];
        
        [self addSubview:self.titleView];
        [self.titleView addSubview:self.titleLb];
        [self.titleView addSubview:self.logoImageView];
        [self.titleLb addSubview:self.lineView];
        [self addSubview:self.valueLb];
        [self addSubview:self.btmTitle];
        [self addSubview:self.canelBtn];
        [self addSubview:self.fixedBtn];
        
        [self addlayout:value title:title type:type];
    }
    return self;
}

- (void)addlayout:(NSString*)value title:(NSString*)title type:(XFAlertViewShowType)type;{
    typeof(self)sself = self;
    
    [self.titleView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(sself.mas_centerX);
        make.width.equalTo(sself);
        make.top.equalTo(sself);
        make.height.equalTo(@30);
        
    }];
    
    [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.bottom.equalTo(sself.titleView);
        make.centerX.equalTo(sself.titleView);
        make.width.lessThanOrEqualTo(sself.titleView);
    }];
    
    CGFloat scale = self.logoImageView.image.size.width / self.logoImageView.image.size.height;
    [self.logoImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(sself.titleView).offset(2);
        make.bottom.equalTo(sself.titleView).offset(-2);
        make.right.equalTo(sself.titleLb.mas_left).offset(-6);
        make.width.equalTo(sself.logoImageView.mas_height).multipliedBy(scale);
        
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(sself.titleLb);
        make.height.equalTo(@1);
        make.bottom.equalTo(sself.titleLb).offset(-2);
        
    }];
    
    CGFloat gap = 15;
    [self.valueLb mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_offset(gap);
        make.right.mas_offset(-gap);
        make.top.equalTo(sself.titleView.mas_bottom).offset(gap);
        make.height.lessThanOrEqualTo(@(CGFLOAT_MAX));
        make.height.greaterThanOrEqualTo(@(35));
    }];
    self.valueLb.text = value;
    
    UIView* temp = self.valueLb;
    
    if (title) {
        
        temp = self.btmTitle;
        [self.btmTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.left.right.equalTo(sself.valueLb);
            make.height.lessThanOrEqualTo(@(CGFLOAT_MAX));
            make.top.equalTo(sself.valueLb.mas_bottom).offset(gap/2);
            
        }];
        self.btmTitle.text = title;
    }
    
    CGFloat btn_height = 40;
    switch (type) {
        case XFAlertViewShowTypeAction:{
            //两个
            [self.canelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
               
                make.left.equalTo(sself);
                make.height.equalTo(@(btn_height));
                make.width.equalTo(sself.mas_width).multipliedBy(0.5);
                make.top.equalTo(temp.mas_bottom).offset(gap);
                
            }];
            
            [self.fixedBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.width.height.equalTo(sself.canelBtn);
                make.right.equalTo(sself);
                
            }];
            
            break;
        }
        case XFAlertViewShowTypeNormal:{
            
            [self.canelBtn setTitle:@"知道了" forState:UIControlStateNormal];
            self.canelBtn.backgroundColor = MAINCOLOR;
            [self.canelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.left.equalTo(sself);
                make.height.equalTo(@(btn_height));
                make.width.equalTo(sself.mas_width);
                make.top.equalTo(temp.mas_bottom).offset(gap/2);
                
            }];
            
            break;
        }
        default:
            break;
    }
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    self.showHeight = self.canelBtn.maxY;
}


- (void)showInView:(UIView *)superView{
    
    self.backView.frame = superView.frame;
    
    self.transform = CGAffineTransformMakeScale(0, 0);
    
    [superView addSubview:self.backView];
    [superView addSubview:self];
    self.backView.alpha = 0;
    
    typeof(self)sself = self;
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(sself.canelBtn.mas_bottom);
        make.centerX.centerY.equalTo(superView);
        make.width.equalTo(@(superView.width * 0.8));
        make.height.lessThanOrEqualTo(@300);
    }];
    
    
    [UIView animateWithDuration:0.2 animations:^{
       
        self.transform = CGAffineTransformIdentity;
        self.backView.alpha = 0.7;
        
    }];
}


- (void)hide{
    [UIView animateWithDuration:0.2 animations:^{
       
        self.transform = CGAffineTransformMakeScale(0.001, 0.001);
        self.backView.alpha = 0;
        
    } completion:^(BOOL finished) {
        [self.backView removeFromSuperview];
        [self removeFromSuperview];
    }];
}


- (void)fixedAction{
    
    [self hide];
    
    if (self.fixedComplate) {
        self.fixedComplate();
    }
}


- (void)setCannelTitle:(NSString *)cannelTitle{
    [self.canelBtn setTitle:cannelTitle forState:UIControlStateNormal];
}

- (void)setAligment:(NSTextAlignment)aligment{
    self.valueLb.textAlignment = aligment;
}

- (void)setFixedTitle:(NSString *)fixedTitle{
    [self.fixedBtn setTitle:fixedTitle forState:UIControlStateNormal];
}

@end
